<?php

// Enter the path that the oauth library is in relation to the php file
// This is required for Yelp authentication.
require_once ('lib/OAuth.php');
require_once ('php/model/place.php');
require_once ('php/util/util.php');
require_once ('php/api/facebook_api_handler.php');
require_once ('php/api/yelp_api_handler.php');
require_once ('php/api/citygrid_search_api_handler.php');

// Combined results from Yelp and CityGrid.
$combinedResults = array();

//Facebook
$facebook = new FacebookApiHandler();
$facebook->connect();
$facebook_response = $facebook->response;

if (!empty($facebook_response)) {
    foreach ($facebook_response as $finding) {
        $place = new Place();
        $place->name = $finding["name"];
        $place->facebook_page_id = $finding["page_id"];
        $place->facebook_fan_count = $finding["fan_count"];
        $place->facebook_categories = $finding["categories"];
        $place->is_facebook = true;
        array_push($combinedResults, $place);
    }
} else {
//    echo "You have no friends who like restaurants.<br/>";
}

//Yelp
$term = urlencode($_GET["term"]);
$location = urlencode($_GET["location"]);
$yelp = new YelpAPIHandler($term, $location);
$yelp->connect();
$yelp_response = $yelp->response;

if (!is_null($yelp_response)) {
    foreach ($yelp_response->businesses as $business) {
        $place = new Place();
        $place->name = $business->name;
        $place->rating = $business->rating;
        $place->yelp_url = $business->url;
        $place->yelp_id = $business->id;
        $place->yelp_image_url = $business->image_url;
        $place->yelp_rating_image_url = $business->rating_img_url;
        $place->yelp_snippet_text = $business->snippet_text;
        $place->yelp_review_count = $business->review_count;
        $place->is_yelp = true;
        array_push($combinedResults, $place);
    }
}

// CityGrid
$citygrid = new CityGridSearchAPIHandler($term, $location);
$citygrid->connect();
$citygrid_response = $citygrid->response;
$results = $citygrid_response->results;

if (!is_null($results)) {
    foreach ($results->locations as $location) {
        $place = new Place();
        $place->name = $location->name;
        $place->rating = $location->rating / 2;
        $place->website = $location->website;
        $place->citygrid_image = $location->image;
        $place->citygrid_id = $location->public_id;
        $place->is_citygrid = true;
        array_push($combinedResults, $place);
    }
}

//sort results alphabetically by __toString method.
//asort($combinedResults);
//sort the results by the rating 
function cmp($a, $b) {
    return strcmp($b->rating, $a->rating);
}

usort($combinedResults, "cmp");

//get top 3 results based on the sort.  
//eventually we can use our own ranking system to sort out the top 3, for now it's rating.
array_splice($combinedResults,3);

printf("<table class='results' align='center'>");

//printf("<td>Yelp URL</td><td>CityGrid URL</td><td>Rating</td><td>Yelp ID</td><td>CityGrid ID</td><td>Facebook Page ID</td>");
$cleanedResults = combineDuplicatePlaces($combinedResults);
//echo($encodedResults);
foreach ($cleanedResults as $result) {
    printf("<tr>");
    if ($result->is_yelp) {
//        printf("<td style= 'background:url(" . getURL("/images/yelp-large.jpg") .") no-repeat center'>");
        printf("<td style='width:800px'>");
        print ("<a href='$result->yelp_url'>$result->name</a>" . "<br />");
        print ($result->yelp_id . "</br>");
        print (is_null($result->yelp_image_url) ? "" : "<img src='" . $result->yelp_image_url . "'>") . "<br />";
        print (is_null($result->yelp_rating_image_url) ? "" : "<img src='". getURL("/images/yelp-word-small.png") ."'> <br /> <img src='" . $result->yelp_rating_image_url . "'> ($result->yelp_review_count)") . "<br />";
        print (is_null($result->yelp_snippet_text) ? "" : $result->yelp_snippet_text . "<a href='$result->yelp_url'>Read More...</a> </br>");
        print ("<button id=$result->yelp_id type='button' onClick=\"beenThere('".$result->yelp_id."');\">Wait I've Been Here</button>");
        printf("</td>");
    } elseif ($result->is_citygrid) {
        printf("<td style='width:400px'>");
        print $result->name . "<br />";
        print (is_null($result->citygrid_image) ? "" : "<img width='100' height='100' src='" . $result->citygrid_image . "'>") . "<br />";
        print (is_null($result->rating) ? "" : "<img src='" . getURL("/images/citygrid-logo-small.png") . "'> <br />". $result->rating) . "<br />";
        print ("<button id=$result->citygrid_id type='button' onClick=\"beenThere('".$result->citygrid_id."');\">Wait I've Been Here</button>");
        printf("</td>");
    } 
    printf("</tr>");
}

printf("</table>");
?>
